<?php
require_once 'db_connection.php';

function insert_entry($type, $children){
  $columns = "(type";
  $values = "('$type'";
  foreach ($children as $child){  
    $name = preg_replace('/\s+/', '', $child);
    $columns = $columns . ", " . $name;
    $values = $values . ", " . "'1'";
  }
  $t = date( 'Y-m-d H:i:s');
  $values = $values . ", '$t')";
  $columns = $columns . ", date)";
  
  $query = "INSERT INTO archives " . $columns . " VALUES" . $values;
  mysql_query($query);
}

function get_all_entries(){
  $to_return = array();
  $query = 'SELECT * FROM archives';
  $result = mysql_query($query);
  $num_rows = mysql_num_rows($result);
  for ($i = 0; $i < $num_rows; ++$i){
    $object = mysql_fetch_assoc($result);
    $to_return[] = $object;
  }
  return $to_return;
}

// takes full name as in the id eg: "Angela Pickles"
function get_entries_by_name($n){
  $name = preg_replace('/\s+/', '', $n); //removes whitespaces from string
  $query = "SELECT * FROM archives WHERE $name=1";
  $result = mysql_query($query);
  $num_rows = mysql_num_rows($result);
  for ($i = 0; $i < $num_rows; ++$i){
    $object = mysql_fetch_assoc($result);
    $to_return[] = $object;
  }
  return $to_return;  
}

function query_page_by_name_types($names,$types){
  $parsed_names = array();
  $names_query = "( ";
  for($i = 0; $i < count($names); $i++){ 
  if ($i == 0){
    $name = preg_replace('/\s+/', '', $names[$i]);
    $names_query = $names_query . $name . "=1";
  }
  else{
    $name = preg_replace('/\s+/', '', $names[$i]);
    $names_query = $names_query . " OR " . $name . "=1";
  }
  }
  $names_query = $names_query . " )";
  
  $types_query = "(";
  
  for($j = 0; $j < count($types); $j++){
    if($j == 0){
      $type = $types[$j];
      $types_query = $types_query . ' type=' . "'$type'";
    }
    else{
      $type = $types[$j];
      $types_query = $types_query . ' OR type=' . "'$type'";
    }
  }
  
  $types_query = $types_query . ")";

  $query = "SELECT * FROM archives  WHERE " . $names_query . " AND " . $types_query ."ORDER BY date DESC LIMIT 30";
  $result = mysql_query($query);
  $num_rows = mysql_num_rows($result);
  for ($i = 0; $i < $num_rows; ++$i){
    $object = mysql_fetch_assoc($result);
    $to_return[] = $object;
  }
  return $to_return;  
}

function get_page_of_entries(){
  $to_return = array();
  $query = 'SELECT * FROM archives ORDER BY date DESC LIMIT 30';
  $result = mysql_query($query);
  $num_rows = mysql_num_rows($result);
  for ($i = 0; $i < $num_rows; ++$i){
    $object = mysql_fetch_assoc($result);
    $to_return[] = $object;
  }
  return $to_return;
}


//PAGINATION IS STILL NOT FINISHED
// function get_page_of_entries_by_name($n){
//   $name = preg_replace('/\s+/', '', $n); //removes whitespaces from string
//   $query = "SELECT * FROM archives WHERE $name=1 ORDER BY date DESC LIMIT 10";
//   $result = mysql_query($query);
//   $num_rows = mysql_num_rows($result);
//   for ($i = 0; $i < $num_rows; ++$i){
//     $object = mysql_fetch_assoc($result);
//     $to_return[] = $object;
//   }
//   return $to_return;  
// }


$operation = $_GET['operation'];

if($operation == ""){
  $operation = $_POST['operation'];
}

//Very basic controller
switch($operation){
  case('get_all_entries'):
    $entries = get_all_entries();
    echo json_encode($entries);
    break;
    
  case('get_entries_by_name'):
    $name = $_GET['name'];
    $entries = get_entries_by_name($name);
    echo json_encode($entries);
    break;
  
  case('query_page_by_name_types'):
    $names = $_POST['names'];
    $types = $_POST['types'];
    $entries = query_page_by_name_types($names, $types);
    echo json_encode($entries);
    break;
    
  case('get_page_of_entries'):
    $entries = get_page_of_entries();
    echo json_encode($entries);
    break;
    
  // case('get_page_of_entries_by_name'):
  //   $name = $_GET['name'];
  //   $entries = get_page_of_entries_by_name($name);
  //   echo json_encode($entries);
  //   break;
    
  case('insert_entry'):
    $type = $_POST['type'];
    $arr = $_POST['names'];
    // $names = json_decode($arr);
    insert_entry($type, $arr);
    break;
    
  default:
    break;
}



// print_r(get_entries_by_name('Kimi Finster'));
// print_r(get_page_of_entries());
// $children = Array('ChuckFinster', 'AngelicaPickles');
// echo insert_entry('lunch_report', $children)
// $names = array("ChuckFinster", "AngelicaPickles");
// $types = array("lunch_report", "share_story");
// $to_return = query_page_by_name_types($names, $types);
// print_r( json_encode($to_return));

?>
